//
//  ViewController.m
//  Test
//
//  Created by xueshan1 on 2018/4/27.
//  Copyright © 2018年 xueshan1. All rights reserved.
//

#import "ViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import <objc/runtime.h>
#import <objc/message.h>

#import "UIView+Externsion.h"

#import "MyTableView.h"
 
#import "ZCBaseModel/ZCBaseModel.h"

#import <YYModel.h>


#define  kWidth [UIScreen mainScreen].bounds.size.width
#define  kHeight [UIScreen mainScreen].bounds.size.height
#define  KWindow [UIApplication sharedApplication].delegate.window


#define PYMargin 20 // 默认边距

#import <MJRefresh.h>
#import "AAA.h"
#import "SFSwipeTableView.h"
#import "CustomSegmentControl.h"


@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,CALayerDelegate>
{
    CATiledLayer *_tiledLayer;
    
}

@property(nonatomic,strong)NSMutableArray *dataArray;

@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UITextField *tf;
@property (weak, nonatomic) IBOutlet MyTableView *tb;
@property(nonatomic,strong)UITextField *bar;

@property(nonatomic,strong)CAShapeLayer *shapeLayer;
@property (nonatomic, strong) CustomSegmentControl * segmentBar;
@property(nonatomic,strong)SFSwipeTableView *swipeView;
@property(nonatomic,assign)CGFloat navHeight;

 @end


@implementation ViewController

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
 
    NSDictionary *dic = @{@"name":@"哈哈哈哈3"};
    
    [_tb yy_modelSetWithDictionary:dic];
    
   
}

- (void)viewDidAppear:(BOOL)animated{
    
  
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tb.delegate = self;
    self.tb.dataSource = self;
    self.tb.frame = CGRectMake(100, 100, 200, 100);
    self.tb.hidden = YES;
    
    
//    [self cutImageAndSave];
//    [self addTiledLayer];

    self.navHeight = 0;
    [self testSwipeView];
}
//
- (void)testSwipeView{
    UIImageView *imageV= [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 160)];
    imageV.image = [UIImage imageNamed:@"背景图@2x.png"];
    imageV.contentMode = UIViewContentModeScaleToFill;
    
    
    SFSwipeTableView *swipeView = [[SFSwipeTableView alloc]initWithFrame:CGRectMake(0, self.navHeight, kWidth, kHeight - self.navHeight)];
    _swipeView = swipeView;
    swipeView.itemViews = @[[self getTableView],[self getTableView],[self getTableView]];
    swipeView.headerView = imageV;
    swipeView.segmentBarView = self.segmentBar;
    swipeView.delegate = self;
    [self.view addSubview:swipeView];
    
    
}

- (CustomSegmentControl * )segmentBar {
    if (nil == _segmentBar) {
        self.segmentBar = [[CustomSegmentControl alloc]initWithItems:@[@"Item0",@"Item1",@"Item2"]];
        _segmentBar.size = CGSizeMake(kWidth, 40);
        _segmentBar.font = [UIFont systemFontOfSize:15];
        _segmentBar.textColor = RGBColor(100, 100, 100);
        _segmentBar.selectedTextColor = RGBColor(0, 0, 0);
        _segmentBar.backgroundColor = RGBColor(249, 251, 198);
        _segmentBar.selectionIndicatorColor = RGBColor(249, 104, 92);
        _segmentBar.selectedSegmentIndex = 0;
        [_segmentBar addTarget:self action:@selector(changeSwipeViewIndex:) forControlEvents:UIControlEventValueChanged];
    }
    return _segmentBar;
}
//tab bar ->改变 scrollView
- (void)changeSwipeViewIndex:(UISegmentedControl *)seg {
 
    [_swipeView scrollToItemViewAtIndex:seg.selectedSegmentIndex animated:NO];
    
}

- (void)swipeTableViewCurrentItemViewChanged:(UITableView *_Nullable)currentItemView currentIndex:(NSInteger)currentIndex{
    self.segmentBar.selectedSegmentIndex = currentIndex;
    
}


- (UITableView *)getTableView{
    UITableView *tb = [[UITableView alloc]initWithFrame:_swipeView.bounds];
    tb.dataSource = self;

    tb.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [tb.mj_header endRefreshing];
        });
    }];
    tb.mj_header.ignoredScrollViewContentInsetTop = 200;

    tb.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [tb.mj_footer endRefreshing];
        });
    }];

    
    return tb;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return arc4random()%20 + 5;
    
    return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ce"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ce"];
    }
    cell.textLabel.text = [NSString stringWithFormat:@" -- %ld" , indexPath.row];
    
    return cell;
}





@end











